package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-07-13
 * Time: 10:50
 */
//通过懒汉模式实现单例模式
class SingletonLazy {
    volatile private static SingletonLazy instace = null;

    public static SingletonLazy getInstance() {
        if (instace == null) {//这个if条件判断是否需要加锁，如果对象已经存在，就不必要进行加锁了，此时本身就是线程安全的。
            synchronized (SingletonLazy.class) {
                if (instace == null) {//这个if条件判断是否创建对象
                    instace =  new SingletonLazy();
                }
            }
        }
        return instace;
    }
    private SingletonLazy() { }

}
public class ThreadDemo18 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
